import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/index.vue') // 路由懒加载  注释表示打包分类
  },
  {
    path: '/reg',
    name: 'Register',
    component: () => import('../views/Register/index.vue') // 路由懒加载  注释表示打包分类
  },
  {
    path: '/',
    component: () => import('../views/Layout/index.vue'),
    children: [
      {
        path: '',
        component: () => import('../views/Home/index.vue')
      },
      {
        path: 'artcategory',
        component: () => import('../views/Art/artCategory.vue')
      },
      {
        path: 'artlist',
        component: () => import('../views/Art/artList.vue')
      },
      {
        path: 'userinfo',
        component: () => import('../views/User/userInfo.vue')
      },
      {
        path: 'changeavatar',
        component: () => import('../views/User/changeAvatar.vue')
      },
      {
        path: 'resetpwd',
        component: () => import('../views/User/resetPwd.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

// 设置路由前置守卫
// 路由拦截：只要路由值发生改变就会触发

router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '/reg']

  if (whiteList.includes(to.path)) {
    next()
  } else {
    // 判断是否有token
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
